(function (vc) {
    vc.extends({
        data: {
            ttsOweFeeCallableInfo: {
                oweFees: [],
                curOweFee: {},
                tts: [],
                ttsPath: '',
                sendFlag: '',
                sendMsg: '',
                tmpOweFees: [],
                callState: ''
            }
        },
        _initMethod: function () {
            let _oweFee = vc.getData('TTS_OWE_FEE_DATA').oweFee;
            if (!_oweFee || _oweFee.length < 1) {
                vc.toast('未选择欠费房屋');
                return;
            }
            $that.ttsOweFeeCallableInfo.oweFees = _oweFee;
        },
        _initEvent: function () {
            vc.on('ttsOweFeeCallable', 'TalkingStart', function () {
                $that.ttsOweFeeCallableInfo.callState = 'TalkingStart';

                let _tel = $that.ttsOweFeeCallableInfo.ownerTel;

                setTimeout(function () {
                    vc.emit('searchCommunityDataModel', 'playTts', {
                        url: $that.ttsOweFeeCallableInfo.ttsPath
                    })
                }, 2 * 1000);

                setTimeout(function () {
                    if (_tel != $that.ttsOweFeeCallableInfo.ownerTel) {
                        return;
                    }
                    vc.emit('searchCommunityDataModel', 'hangup', {
                    })
                }, 60 * 1000);

            });
            vc.on('ttsOweFeeCallable', 'TalkingEnd', function () {
                $that.ttsOweFeeCallableInfo.callState = 'TalkingEnd';
                $that._changeOweFeeState($that.ttsOweFeeCallableInfo.curOweFee.payerObjId, "C");
                let _curOwe = $that.ttsOweFeeCallableInfo.curOweFee;
                let _sendMsg = $that.ttsOweFeeCallableInfo.sendMsg;

                _sendMsg = _sendMsg.replace('${house}', _curOwe.payerObjName).replace('${date}', vc.dateFormat(_curOwe.endTime))
                    .replace('${date2}', vc.dateFormat(_curOwe.deadlineTime));
                _sendMsg = _sendMsg.replace('${mount}', _curOwe.amountOwed);

                setTimeout(function () {
                    vc.emit('searchCommunityDataModel', 'sendTelMsg', {
                        tel: _curOwe.ownerTel,
                        msgText: _sendMsg
                    })
                    $that._doTtsOweFee();
                }, 5000)
            });


        },
        methods: {
            _openChooseTtsAndStart: function () {
                $('#chooseTelTtsModel').modal('show');

                $that._loadTelTts();

            },
            _startTtsOweFeeModal: function () {

                let msg = "";
                if (!$that.ttsOweFeeCallableInfo.ttsPath) {
                    msg = "未选择语音";
                }

                if (!$that.ttsOweFeeCallableInfo.sendFlag) {
                    msg = "未选择是否发送短信";
                }

                if ($that.ttsOweFeeCallableInfo.sendFlag == 'Y') {
                    if (!$that.ttsOweFeeCallableInfo.sendMsg) {
                        msg = "未填写短信内容";
                    }
                }

                if (msg) {
                    vc.toast(msg);
                    return;
                }
                $('#chooseTelTtsModel').modal('hide');
                let _oweFees = $that.ttsOweFeeCallableInfo.oweFees;
                let _tmpOweFees = [];

                _oweFees.forEach(_of => {
                    if (_of.state == 'W') {
                        _tmpOweFees.push(_of);
                    }
                });

                if (_tmpOweFees.length < 1) {
                    vc.toast("没有需要催缴的数据");
                    return;
                }

                $that.ttsOweFeeCallableInfo.tmpOweFees = _tmpOweFees;

                setTimeout(function () {
                    $('#ttsOweFeeCallableModel').modal('show');
                    $that._doTtsOweFee();
                }, 1000)


            },

            _doTtsOweFee: async function () {
                let _oweFees = $that.ttsOweFeeCallableInfo.tmpOweFees;
                let _curOweFee = _oweFees.shift();

                if (!_curOweFee) {
                    $('#ttsOweFeeCallableModel').modal('hide');
                    return;
                }

                $that.ttsOweFeeCallableInfo.callState = 'callWait';
                _curOweFee.stateName = '即将开始催缴（等待10秒）';
                $that.ttsOweFeeCallableInfo.curOweFee = _curOweFee;
                await $that.sleep(10);
                $that._changeOweFeeState($that.ttsOweFeeCallableInfo.curOweFee.payerObjId, "D")

                vc.emit('searchCommunityDataModel', 'callTel', {
                    tel: _curOweFee.ownerTel,
                    componentName: 'ttsOweFeeCallable'
                });

                $that.ttsOweFeeCallableInfo.callState = 'callTel';

            },

            _backTtsOweFee: function () {
                vc.removeData('TTS_OWE_FEE_DATA');
                vc.goBack();
            },
            sleep: function (seconds) {
                return new Promise(resolve => setTimeout(resolve, seconds * 1000));
            },
            _loadTelTts: function (_page, _row) {
                let param = {
                    params: {
                        page: 1,
                        row: 1,
                        communityId: vc.getCurrentCommunity().communityId,
                        iotApiCode: 'queryTelTtsBmoImpl'
                    }
                };
                vc.http.apiGet('/iot.getOpenApi',
                    param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        if (_json.code != 0) {
                            return;
                        }
                        $that.ttsOweFeeCallableInfo.tts = _json.data;

                    }, function () {
                        console.log('请求失败处理');
                    }
                );
            },
            _changeOweFeeState: function (_payerObjId, _state) {
                let _oweFees = $that.ttsOweFeeCallableInfo.oweFees;

                _oweFees.forEach(_of => {
                    if (_of.payerObjId == _payerObjId) {
                        _of.state = _state;
                    }
                });
            },
            _removeOweFee: function (_oweFee) {
                let _tempOweFees = [];
                let _oweFees = $that.ttsOweFeeCallableInfo.oweFees;

                _oweFees.forEach(_of => {
                    if (_oweFee.payerObjId != _of.payerObjId) {
                        _tempOweFees.push(_of);
                    }
                });
                $that.ttsOweFeeCallableInfo.oweFees = _tempOweFees;
            }
        }
    });
})(window.vc);